/*
 * kylin-os-manager
 *
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QWidget>
#include <QTranslator>
#include <QLibraryInfo>
#include <QDir>
#include <QStandardPaths>

#include "kyplugin.h"
#include "kymanager_plugin.h"
#include "mainwindow.h"
#include <fcntl.h>
#include <syslog.h>
#include <QLocale>
#include "log.hpp"

//#define ENABLE_DEBUG

using namespace kdk::kabase;

int main(int argc, char *argv[])
{
    qInstallMessageHandler(kdk::kabase::Log::logOutput);

// 修改分辨率的显示
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    qDebug() << "setAttribute success";
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    qDebug() << "setHighDpiScaleFactorRoundingPolicy success";
#endif

    QApplication app(argc, argv);

	kyplugin::KyPlugin plugin;
	plugin.acceptProviderType<KyManagerPluginProvider>();

#ifdef ENABLE_DEBUG
	QDir dir(app.applicationDirPath());
	dir.cdUp();
	dir.cd("plugins");
	plugin.loadFromFolder(dir.absolutePath().toStdString(), true);
#else
	plugin.loadFromFolder("/opt/kylin-os-manager/plugins/");
#endif

    std::vector<KyManagerPluginProvider *> plugins;
    plugin.getProviders(plugins);
    QVector<KyManagerPluginProvider *> tmp = QVector<KyManagerPluginProvider *>::fromStdVector(plugins);

    //检测单例
    QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
    int singleInstanceFd =
        open(QString(homePath.at(0) + "/.config/kylin-os-manager%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
             O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (singleInstanceFd < 0) {
        exit(1);
    }
    if (lockf(singleInstanceFd, F_TLOCK, 0)) {
        syslog(LOG_ERR, "Can't lock single file, kylin-os-manager is already running!");
        exit(0);
    }

    /* 加载应用翻译文件 */
    QString kylinFileCrush = "/usr/share/kylin-os-manager/translations";
    QTranslator trans_global;
    if (!trans_global.load(QLocale(), "kylin-os-manager", "_", kylinFileCrush)) {
        qWarning() << "main Load global translations file" << QLocale() << "failed!";
    } else {
        app.installTranslator(&trans_global);
    }
    /* 加载Qt翻译文件 */
    QTranslator trans_qt;
    QString qtTransPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
    if (!trans_qt.load(QLocale(), "qt", "_", qtTransPath)) {
        qWarning() << "main Load qt translations file" << QLocale() << "failed!";
    } else {
        app.installTranslator(&trans_qt);
    }
    //加载SDK的翻译
    QString locale = QLocale::system().name();
    QTranslator sdk_trans;
    if (!sdk_trans.load(":/translations/gui_" + locale + ".qm")) {
        qDebug() << "Load sdk translation file failed!";
    } else {
        app.installTranslator(&sdk_trans);
    }

    MainWindow W(tmp);
    W.setSingleFileFd(singleInstanceFd);
    W.show();

    return app.exec();
}
